package com.shenmazong.dead.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

/**
 * @author 军哥
 * @version 1.0
 * @description: 发送延迟消息
 * @date 2022/2/9 19:36
 */

@RestController
@Slf4j
public class SendMessageController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping(value = "/send")
    public String send(@RequestParam("msg") String msg) {

        String messageId = UUID.randomUUID().toString();
        log.warn("send messageId:"+messageId);

        Message message = MessageBuilder.withBody(msg.getBytes(StandardCharsets.UTF_8)).setMessageId(messageId).build();

        rabbitTemplate.convertAndSend("businessExchange", "", message);

        return msg;
    }
}
